#include "mainwindow.h"
#include "qcustomplot.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 为显示设置模型
    auto data = objWBData.GetModel();
    ui->tableView->setModel(data);
    ui->tableView->show();
    objWBData.LoadData("acqter230309A.db");
}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::on_btnLoadDb_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(nullptr,
                                                    "Open File", "~/", "All Files (*.db)");
    objWBData.LoadData(fileName);
    QStringList strTables = objWBData.GetTableNames();
    for(auto item:strTables)
    {
        ui->comboBox_table->addItem(item, item);
    }

    objWBData.SetTableName(strTables[0]);
}

void MainWindow::on_comboBox_table_currentIndexChanged(const QString &arg1)
{
    ui->comboBox_node->clear();
    objWBData.SetTableName(arg1);
    QStringList lstNames = objWBData.GetNodeNames();
    ui->comboBox_node->addItem("all", "*");
    for(auto item:lstNames)
    {
        ui->comboBox_node->addItem(item, item);
    }
}

void MainWindow::on_comboBox_node_currentIndexChanged(const QString &arg1)
{
    objWBData.SetNodeName(ui->comboBox_node->currentData().toString());
    QStringList strHeads = objWBData.GetTableHeads();

    // 清空绘制的图像
    QLayoutItem *item;
    while ((item = ui->verticalLayout_Draw1->takeAt(0)) != nullptr) {
        if (item->widget() != nullptr) {
            item->widget()->deleteLater();
        }
        delete item;
    }

    for(int i=3;i<strHeads.size();i++)
    {
        QSqlQuery query = objWBData.GetColData(strHeads[i]);

        QVector<double> xData, yData;

        int x=0;
        // 执行SELECT语句获取数据
        while (query.next()) {
            xData.append(x++);
            yData.append(query.value(0).toDouble());
        }

        // 创建QCustomPlot对象
        QCustomPlot *customPlot = new QCustomPlot;
        customPlot->setMinimumHeight(150);
        ui->verticalLayout_Draw1->addWidget(customPlot);

        // 设置标题
        QString plotTitle = strHeads[i];
        QFont plotTitleFont("Helvetica", 12, QFont::Bold);
        QCPTextElement *title = new QCPTextElement(customPlot, plotTitle, plotTitleFont);
        customPlot->plotLayout()->insertRow(0);  // 插入一行用于存放title
        customPlot->plotLayout()->addElement(0, 0, title);

        customPlot->show();

        // 添加一个图层
        customPlot->addGraph();

        // 设置图层的数据
        customPlot->graph(0)->setData(xData, yData);

        // 设置图层的样式
        customPlot->graph(0)->setPen(QPen(Qt::blue));
        customPlot->xAxis->setLabel("x轴");
        customPlot->yAxis->setLabel("y轴");

        // 自适应x、y轴范围
        customPlot->rescaleAxes();

        // 允许鼠标旋转和缩放
        customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

        // 重新绘制图层
        customPlot->replot();
    }
}
